/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.eagleeyes.hoverbuttons.jogl;

import javax.media.opengl.GL;
import org.eagleeyes.hoverbuttons.EEFocusable;
import org.eagleeyes.hoverbuttons.EEFocusableListener;
import org.eagleeyes.hoverbuttons.NullEEFocusableListener;

/**
 *
 * @author Jesse Bahr
 */
public class GLEEButton extends EEFocusable implements EEFocusableListener{
  /**
   * the default color of the highlight is semi-transparent white.
   */
  public static final float[] DEFAULT_COLOR = {1f, 1f, 1f, .8f};
  private static final float BUFFER_LEN = 2f;
  
  private float percent;
  private float[] color;
  private EEFocusableListener buttonListener;

  private void init(){
    super.setListener(this);
    percent = -1f;
    setColor(DEFAULT_COLOR);
    buttonListener = new NullEEFocusableListener();
  }

  public GLEEButton(){
    super();
    setWidth(32);
    setHeight(32);
    init();
  }
  
  public GLEEButton(int x, int y, int width, int height){
    super();
    init();
    setX(x);
    setY(y);
    setWidth(width);
    setHeight(height);
  }
  
  
  
  /**
   * draw the appropriate highlights on the button.  This makes the assumption that we're in 2D mode in JOGL.<br/>
   * overriding methods should call super.draw(gl).
   * @param gl
   */
  public void draw(GL gl){
    //percent < 0 indicates that it's not hovered currently.
    if (percent >= 0f){
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glPushMatrix();

      float transx = (float) (getX() + getWidth() / 2);
      float transy = (float) (getY() + getHeight() / 2);
      gl.glTranslatef(transx, transy, 0f);

      float scalex = ((float) getWidth()) - BUFFER_LEN - BUFFER_LEN;
      float scaley = ((float) getHeight()) - BUFFER_LEN - BUFFER_LEN;
      gl.glScalef(scalex, scaley, 1f);
      
      float oneMinusPerc = 1f - (percent * percent);
      gl.glScalef(oneMinusPerc, oneMinusPerc, oneMinusPerc);
      gl.glColor4fv(color, 0);
      
      //temp
      /*
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(-.5f, -.5f, 0f);
        gl.glVertex3f(-.5f, .5f, 0f);
        gl.glVertex3f(.5f, .5f, 0f);
        gl.glVertex3f(.5f, -.5f, 0f);
      gl.glEnd();
       */

      //lower-left
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(-.5f, -.5f, 0f);
        gl.glVertex3f(-.2f, -.5f, 0f);
        gl.glVertex3f(-.2f, -.4f, 0f);
        gl.glVertex3f(-.5f, -.4f, 0f);
      gl.glEnd();
      
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(-.5f, -.4f, 0f);
        gl.glVertex3f(-.4f, -.4f, 0f);
        gl.glVertex3f(-.4f, -.2f, 0f);
        gl.glVertex3f(-.5f, -.2f, 0f);
      gl.glEnd();

      //upper-left
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(-.5f, .5f, 0f);
        gl.glVertex3f(-.2f, .5f, 0f);
        gl.glVertex3f(-.2f, .4f, 0f);
        gl.glVertex3f(-.5f, .4f, 0f);
      gl.glEnd();
      
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(-.5f, .4f, 0f);
        gl.glVertex3f(-.4f, .4f, 0f);
        gl.glVertex3f(-.4f, .2f, 0f);
        gl.glVertex3f(-.5f, .2f, 0f);
      gl.glEnd();
      

      //lower-right
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(.5f, -.5f, 0f);
        gl.glVertex3f(.2f, -.5f, 0f);
        gl.glVertex3f(.2f, -.4f, 0f);
        gl.glVertex3f(.5f, -.4f, 0f);
      gl.glEnd();
      
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(.5f, -.4f, 0f);
        gl.glVertex3f(.4f, -.4f, 0f);
        gl.glVertex3f(.4f, -.2f, 0f);
        gl.glVertex3f(.5f, -.2f, 0f);
      gl.glEnd();

      //upper-right
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(.5f, .5f, 0f);
        gl.glVertex3f(.2f, .5f, 0f);
        gl.glVertex3f(.2f, .4f, 0f);
        gl.glVertex3f(.5f, .4f, 0f);
      gl.glEnd();
      
      gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex3f(.5f, .4f, 0f);
        gl.glVertex3f(.4f, .4f, 0f);
        gl.glVertex3f(.4f, .2f, 0f);
        gl.glVertex3f(.5f, .2f, 0f);
      gl.glEnd();
      
      
      gl.glPopMatrix();
    }
  }

  public void focus() {
    buttonListener.focus();
    percent = -1f;
  }

  public void highlight(float percent) {
    this.percent = percent;
    buttonListener.highlight(percent);
  }
  
  public void unhighlight(){
    percent = -1f;
    buttonListener.unhighlight();
  }
  

  public float[] getColor() {
    return color;
  }

  /**
   * set the color of the highlight, which must be a float[4]
   * @param color the color of the highlight to draw
   * @throws IllegalArgumentException if the array length is not 4
   */
  public void setColor(float[] color) {
    if (color.length != 4)
      throw new IllegalArgumentException("Invalid color length: " + color.length);
    this.color = color;
  }

  public EEFocusableListener getButtonListener() {
    return buttonListener;
  }

  public void setButtonListener(EEFocusableListener buttonListener) {
    this.buttonListener = buttonListener;
  }
  
  protected float getPercent(){
    return percent;
  }
  
  @Override public void setListener(EEFocusableListener l){
    setButtonListener(buttonListener);
  }
}
